//
//  UserAccountViewModel.swift
//  sinaDemo
//
//  Created by mac on 15/12/13.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

class UserAccountViewModel: NSObject {
    
    // 定义用户模型对象的属性
    var userAccount: UserAccount?
    
    override init() {
        // 先构造本类,再构造父类
        userAccount = UserAccount.accoun()
        super.init()
    }
    
    // 用户是否登录
    var userLogin: Bool {
        return userAccount?.access_token != nil
    }
    
    // 用户头像的url
    var iconUrl: NSURL? {
        let url = NSURL(string: userAccount?.avatar_large ?? "")
        return url
    }
    
    // 用户名
    var userName: String? {
        return userAccount?.name
    }
    
    // token
    var access_token: String? {
        return userAccount?.access_token
    }
    
    func getAccessToken(code: String, finish:(() ->())) {
        var params = NSMutableDictionary()
        params["client_id"] = client_id
        params["client_secret"] = scret
        params["grant_type"] = "authorization_code"
        params["code"] = code
        params["redirect_uri"] = redirect_uri
        // 发送请求
        let afn = AFHTTPSessionManager()
        afn.responseSerializer.acceptableContentTypes?.insert("text/plain")
        afn.POST("https://api.weibo.com/oauth2/access_token", parameters: params, progress: nil, success: { (_, data) -> Void in
            SVProgressHUD.dismiss()
            if let dic = data as? [String : AnyObject] {
                // 字典转模型
                let account = UserAccount(dic: dic)
                print(account)
                // 存进沙盒,归档
                // 再次发送请求
                self.getUserInfo(account, finish: finish)
            }
            
            
            }) { (_, error) -> Void in
                SVProgressHUD.showErrorWithStatus("网络繁忙,请稍后再试!!")
                print(error)
        }
    }
    
    private func getUserInfo(account: UserAccount, finish:(() ->())) {
        let afn = AFHTTPSessionManager()
        // https://api.weibo.com/2/users/show.json
        if let access_token = account.access_token, uid = account.uid {
            let params = ["access_token" : access_token, "uid" : uid]
            print(access_token,uid)
            // 发送请求
            afn.GET("https://api.weibo.com/2/users/show.json", parameters: params, progress: { (_) -> Void in
                
                }, success: { (_, data) -> Void in
                    // 登陆成功
                    SVProgressHUD.dismiss()
                    if let dic = data {
                        account.name = dic["name"] as? String
                        account.avatar_large = dic["avatar_large"] as? String
                        // 存入沙盒
                        account.save()
                        // 切换控制器
                        finish()
                        
                    }
                    print(data)
                }, failure: { (_, error) -> Void in
                    print(error)
            })
            
        }
    }

}
